package com.learn.mvc.handler;

import com.learn.mvc.entity.Movie;
import com.learn.mvc.service.api.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
public class MovieHandler {

    @Autowired
    private MovieService movieService;


    @RequestMapping(value = "/movie", method = RequestMethod.GET)
    public String getMovieList(Model model) {
        List<Movie> movieList = movieService.getAll();
        model.addAttribute("movieList", movieList);
        return "movie-list";
    }

    @RequestMapping(value = "/movie/{movieId}", method = RequestMethod.DELETE)
    public String removeMovie(
            @PathVariable("movieId") String movieId
    ) {
        movieService.removeMovieById(movieId);
        return "redirect:/movie";
    }

    @RequestMapping(value = "/movie", method = RequestMethod.POST)
    public String saveMovie(
            Movie movie
    ) {
        movieService.saveMovie(movie);
        return "redirect:/movie";
    }

    @RequestMapping(value = "/movie/{movieId}",method = RequestMethod.GET)
    public String editMovie(Model model, @PathVariable("movieId") String movieId) {
        Movie movie = movieService.getMovieById(movieId);
        model.addAttribute("movie", movie);
        return "movie-edit";
    }
    
    @RequestMapping(value = "/movie", method = RequestMethod.PUT)
    public String updateMovie(Movie movie) {
        movieService.updateMovie(movie);
        return "redirect:/movie";
    }


}
